package session.stateless;

import entity.AddressEntity;
import entity.BranchEntity;
import entity.BranchAddressEntity;
import entity.FnBBranchEntity;
import entity.BranchOperatingHoursEntity;
import entity.CuisinePrefEntity;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import util.enumeration.CuisineType;
import util.enumeration.FnbType;

@Stateless
@LocalBean
public class BranchSessionBean {

    @PersistenceContext
    private EntityManager entityManager;

    public boolean addBranch(String branchName, int postalCode, String houseNumber,
            String streetName, String buildingName, String contactNumber,
            String contactPerson, FnbType fnbType, String isHalal, String priceRange, String remarks, List<CuisineType> selectedCuisine) {
        try {
            //Branch address
            AddressEntity branchAddress = new AddressEntity();
            branchAddress.setPostalCode(postalCode);
            branchAddress.setHouseNumber(houseNumber);
            branchAddress.setStreetName(streetName);
            branchAddress.setBuildingName(buildingName);
            entityManager.persist(branchAddress);
            entityManager.flush();

            //Branch address
            FnBBranchEntity FnB = new FnBBranchEntity();
            FnB.setIsHalal(isHalal);
            FnB.setFnBTypeEnum(fnbType);
            FnB.setFnBType(fnbType.name());
            FnB.setPriceRange(priceRange);
            entityManager.persist(FnB);
            entityManager.flush();

            //Cuisine Pref
            for (int i = 0; i < selectedCuisine.size(); i++) {
                CuisinePrefEntity cp = new CuisinePrefEntity();
                cp.setCuisineTypeEnumId(selectedCuisine.get(i));
                cp.setCuisineType(selectedCuisine.get(i).name());
                cp.setFnbBranch(FnB);
                entityManager.persist(cp);
                entityManager.flush();
            }

            //Branch
            BranchEntity branch = new BranchEntity();
            branch.setBranchName(branchName);
            branch.setContactPerson(contactPerson);
            branch.setContactNumber(contactNumber);
            branch.setBranchAddress(branchAddress);
            branch.setFnBBranch(FnB);
            entityManager.persist(branch);
            entityManager.flush();
            /*
            //Branch Operating Hours
            for (int i = 0; i < operatingList.size(); i++) {
            BranchOperatingHoursEntity detail = new BranchOperatingHoursEntity();
            detail.setBranch(branch);
            detail.setOperatingDay(operatingList.get(i).getDay());
            detail.setStartTime(operatingList.get(i).getOpeningHours());
            detail.setEndTime(operatingList.get(i).getClosingHours());
            entityManager.persist(detail);
            entityManager.flush();
            }
             */
            return true;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public List<FnBBranchEntity> getFnBBranchList() {
        Query query = entityManager.createQuery("SELECT r FROM FnBBranchEntity r");
        return query.getResultList();
    }
    
    //**********************************************************************
    //**********Still need to change to select branch by stakeholder********
    //**********************************************************************
    public List<BranchEntity> viewAllBranch() {
        Query q = entityManager.createQuery("SELECT b FROM BranchEntity b");
        return q.getResultList();
    }
    //**********************************************************************
    //**********************************************************************
    
    //====================== Selection from database with branch ======================
    public List<BranchAddressEntity> viewBranchAddress(BranchEntity b) {
        Query query = entityManager.createQuery("SELECT ba FROM BranchAddress ba, Branch b where b =:branch AND b.branchAddress = ba");
        query.setParameter("branch", b);
        return query.getResultList();
    }

    public List<FnBBranchEntity> viewFnBBranch(BranchEntity b) {
        Query query = entityManager.createQuery("SELECT fnbb FROM FnBBranch fnbb, Branch b where b =:branch AND b.fnBBranch = fnbb");
        query.setParameter("branch", b);
        return query.getResultList();
    }

    public List<BranchOperatingHoursEntity> viewBranchOperatingHours(BranchEntity b) {
        Query query = entityManager.createQuery("SELECT b FROM BranchOperatingHours b where b.branch =:branch");
        query.setParameter("branch", b);
        return query.getResultList();
    }
    
    public List<CuisinePrefEntity> viewCuisinePref(BranchEntity b) {
        Query query = entityManager.createQuery("SELECT c FROM CuisinePref c, FnBBranch fnbB where fnbB.branch =:branch AND c.fnbBranch = fnbB.fnbBranch");
        query.setParameter("branch", b);
        return query.getResultList();
    }
}
